//
//  FKOrderEvaluateVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/25.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKOrderEvaluateVC.h"
#import "FKOrderEvaluateCell.h"
#import "FKCommodityOfEvaluate.h"

@interface FKOrderEvaluateVC ()<FKOrderEvaluateCellDelegate>

@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKOrderEvaluateVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"评价";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self loadData];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithTarget:self action:@selector(commitBtnClicked) text:@"提交" textColor:fkMainColor font:fk_adjustFont(16)];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
}

- (void)commitBtnClicked
{
    [self.view endEditing:true];
    
//    NSMutableArray *array = [NSMutableArray array];
//    for (FKCommodityOfEvaluate *model in self.datas) {
//        NSMutableDictionary *param = [NSMutableDictionary dictionary];
//        [array addObject:param];
//        param[@"order_id"] = model.order_id;
//        param[@"item_id"] = model.item_id;
//        param[@"star"] = model.star;
//        param[@"content"] = model.content;
////
//    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    NSMutableDictionary *fileDict = [NSMutableDictionary dictionary];
    if (self.datas.count > 0) {
        FKCommodityOfEvaluate *model = self.datas.firstObject;
        param[@"order_id"] = model.order_id;
        param[@"item_id"] = model.item_id;
        param[@"star"] = model.star;
        param[@"content"] = model.content;
        [fileDict addEntriesFromDictionary:model.photoDict];
    }
    [SVProgressHUD show];
    [FKHttpTool uploadFile:@"m=App&c=KyOrder&a=setEvaluate" param:param files:fileDict progress:^(NSProgress *progress) {
        [SVProgressHUD showProgress:progress.fractionCompleted];
    } success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showSuccessWithStatus:@"评价成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOrderListVC" object:nil];
            fkPopVC;
        }
    } failure:nil];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKOrderEvaluateCell *cell = [FKOrderEvaluateCell cellWith:tableView];
    cell.commodity = self.datas[indexPath.section];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCommodityOfEvaluate *model = self.datas[indexPath.section];
    return model.cellH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

#pragma mark - FKOrderEvaluateCellDelegate
- (void)orderEvaluateCellPhotoViewChange:(FKOrderEvaluateCell *)cell
{
    [self.tableView reloadData];
}


- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"order_id"] = self.orderId;
    [FKHttpTool postWithSuffix:@"m=App&c=KyOrder&a=getOrderEvaluateInformation" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            self.datas = [FKCommodityOfEvaluate mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            [self.tableView reloadData];
        }
    } failure:nil];
}


@end
